import Router from '../../utils/router';
import base from '../../config/base.js';
import service from '../../services/specialarea.js';
import Record_server from '../../services/record-stat';
import Record from '../../utils/page-record.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    store_id: '',
    goodsList: [],
    loadBloon: true,
    endBloon: true,
    page: 1,
    nomore: true,
    transmit: {
      style: 'position:absolute;width:100%;height:100%;right:0rpx;top:0rpx;opacity:0;',
      value: ''
    },
    isShenhe: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if (options.method && options.method == 'navigateBack') {
      //抽奖进入
      that.entranceLuck(options, that.data.goodsList)
    } else {
      that.setData({
        store_id: wx.getStorageSync('user_info').lastView.seid || '',
        isShenhe: wx.getStorageSync('isShenhe')
      })
      that.specialImgList(1,(list) => {
        that.entranceLuck(options,list)
      })
    }
  },
  //图片列表
  specialImgList: function (page,cb) {
    let that = this;
    let params = {
      offset: 10,
      page: page
    }
    service.getspecialtype(params, function (res) {
      if (res.code && res.code == 1) {
        let strarr = that.data.goodsList.concat(res.data.list);
        that.setData({
          goodsList: strarr,
          endBloon: true,
          page: page + 1,
        })
        if (strarr.length == res.data.count) {
          that.setData({
            loadBloon: false,
            nomore: false
          })
        }
        if(typeof(cb) == 'function'){
          // 抽奖倒计时
          cb(strarr)
        }
      }
      wx.stopPullDownRefresh()
    });
  },
  // 去下属活动列表
  goSpecialList: function (e) {
    Router.go('specialtype', `store_id=${this.data.store_id}&slgsid=${e.currentTarget.dataset.slgsid}`)
  },
  // 从抽奖进入倒计时
  entranceLuck: function(options,list){
    let that = this;
    let inx = Math.floor(Math.random() * (list.length));
    if (options.entrance && list != "") {
      Router.go('specialtype', `store_id=${that.data.store_id}&slgsid=${that.data.goodsList[inx].slgsid}&entrance=${options.entrance}`, 'redirectTo')
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      goodsList: [],
      loadBloon: true,
      endBloon: true,
      nomore: true,
    })
    this.specialImgList(1)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    if (that.data.loadBloon && that.data.endBloon){
      that.setData({
        endBloon: false,
      })
      that.specialImgList(that.data.page)
    }
  },
})